import { effectFn } from "./effect";

class computedImpl{
    private _getter: any;
    private dirty:any = true;
    private _value: any;
    constructor(getter) {
        // 第二个参数为effect的scheduler功能
        this._getter = new effectFn(getter,()=>{
            this.dirty = true
        })
    }
    get value() {
        if(this.dirty) {
            this.dirty = false
            this._value =  this._getter.run();
        }
        return this._value;
    }
}

export function computed(getter) {
    return new computedImpl(getter);
}